package com.daxt.controller.webuse;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.daxt.common.annotations.LoginAuth;
import com.daxt.common.annotations.LoginUser;
import com.daxt.model.dto.Classifys;
import com.daxt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daxt.base.common.util.RedisUtil;
import com.daxt.base.common.util.UserInfoUtil;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.sys.ArchivesFondsMapper;
import com.daxt.mapper.sys.ArchivesTypeMapper;
import com.daxt.mapper.sys.UserFondsMapper;
import com.daxt.model.service.archives.vo.ArchivesFonds;
import com.daxt.model.service.archives.vo.ArchivesType;
import com.daxt.model.service.base.result.UserInfo;
import com.daxt.model.service.base.vo.UserFonds;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "档案类型")
@RestController
@RequestMapping("/web/select")
public class ListController {
	
	@Autowired
	private ArchivesFondsMapper archivesFondsMapper;
	@Autowired
	private ArchivesTypeMapper archivesTypeMapper;
	@Autowired
	private UserFondsMapper fondsMapper;
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private UserInfoUtil userInfoUtil;
	@Autowired
	private UserService userService;
	
	@ApiOperation(value = "全宗号", notes = "") 
	@GetMapping("/fonds") 
	public Result<List<ArchivesFonds>> userInfo(HttpServletRequest request){
		UserInfo loginInfo = userInfoUtil.getLoginInfo(UserInfo.class, redisUtil, request);
		QueryWrapper<UserFonds> queryWrapper=new QueryWrapper<UserFonds>();
		queryWrapper.eq("userId", loginInfo.getId());
		List<UserFonds> userFondsList = fondsMapper.selectList(queryWrapper);
		List<String> ids=new ArrayList<String>();
		List<ArchivesFonds> result=new ArrayList<ArchivesFonds>();
		if(userFondsList!=null&&userFondsList.size()>0) {
			for (UserFonds userFonds : userFondsList) {
				ids.add(userFonds.getFondsId());
			}
			QueryWrapper<ArchivesFonds> queryWarpperFonds=new QueryWrapper<ArchivesFonds>();
			queryWarpperFonds.in("id", ids);
			queryWarpperFonds.orderByAsc("sort");
			result = archivesFondsMapper.selectList(queryWarpperFonds);
		}
		
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", result!=null?result:new ArrayList<ArchivesFonds>());
	}
	
	@ApiOperation(value = "档案类型", notes = "") 
	@PostMapping("/archivesType/{fondsId}")
	@LoginAuth()
	public Result<List<ArchivesType>> editInfo(@ApiIgnore @LoginUser UserInfo loginUser, @PathVariable("fondsId")String fondsId){
		QueryWrapper<ArchivesType> queryWrapper=new QueryWrapper<ArchivesType>();

		//获取用户数据权限
		Classifys c = userService.buildUserClassifys(loginUser);
		List<Long> categoryIds = c.getCategoryIds();//门类ids

		queryWrapper.eq("fondsId", fondsId);

		if(!"admin".equals(loginUser.getUsername()) && !"超级管理员".equals(loginUser.getRole())){
			queryWrapper.in(categoryIds!=null && categoryIds.size()>0,"id",categoryIds);
		}

		List<ArchivesType> selectList = archivesTypeMapper.selectList(queryWrapper);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", selectList);
	}
	
	
}
